package com.ecjtu.stadiumre_servations.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ecjtu.stadiumre_servations.config.RabbitMQConfig;
import com.ecjtu.stadiumre_servations.entity.domain.Booking;
import com.ecjtu.stadiumre_servations.mapper.BookingMapper;
import com.ecjtu.stadiumre_servations.entity.domain.DataAnalysis;
import com.ecjtu.stadiumre_servations.mapper.DataAnalysisMapper;
import com.ecjtu.stadiumre_servations.mapper.GroupActivityMapper;
import com.ecjtu.stadiumre_servations.mapper.GroupMemberMapper;
import com.ecjtu.stadiumre_servations.mapper.GroupRecordMapper;
import com.ecjtu.stadiumre_servations.service.BookingService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class PaymentResultConsumer {
    @Autowired
    private DataAnalysisMapper dataAnalysisMapper;
    @Autowired
    private BookingService bookingService;
    @Autowired
    private GroupActivityMapper groupActivityMapper;
    @Autowired
    private GroupRecordMapper groupRecordMapper;
    @Autowired
    private BookingMapper bookingMapper;
    @Autowired
    private GroupMemberMapper groupMemberMapper;
    @RabbitListener(queues = RabbitMQConfig.PAYMENT_RESULT_QUEUE_NAME)
    public void handlePaymentResult(Map<String, String> paymentResult) {
        try {
            // 先检查 paymentResult 是否为 null
            if (paymentResult == null) {
                System.err.println("接收到的支付结果消息为 null，无法处理");
                return;
            }
            System.out.println("handlePaymentResult");
            // 检查必要的键是否存在
            if (!paymentResult.containsKey("out_trade_no")) {
                System.err.println("支付结果消息中缺少 out_trade_no 字段: " + paymentResult);
                return;
            }
            if (!paymentResult.containsKey("trade_status")) {
                System.err.println("支付结果消息中缺少 trade_status 字段: " + paymentResult);
                return;
            }
            // 获取交易号和交易状态
            String outTradeNo = paymentResult.get("out_trade_no");
            String tradeStatus = paymentResult.get("trade_status");
            System.out.println("收到支付结果消息，交易号: " + outTradeNo + "，状态: " + tradeStatus);
            // 根据订单号找到对应的预约
            Booking booking = bookingService.getById(outTradeNo);
            if (bookingService.getById(outTradeNo)==null) {
                System.out.println("未找到订单号对应的预约: " + outTradeNo);
//                return;
            }
            switch (tradeStatus) {
                case "TRADE_SUCCESS":
                    if (booking != null && "待支付".equals(booking.getBookingStatus())) {
                        System.out.println("TRADE_SUCCESS - 更新预约状态为已支付: " + outTradeNo);
                        booking.setBookingStatus("已支付");
                        LambdaQueryWrapper<DataAnalysis> wrapper = new LambdaQueryWrapper<>();
                        wrapper.eq(DataAnalysis::getStatTime, booking.getBookingEndTime().toLocalDate());
                        if (dataAnalysisMapper.selectCount(wrapper) == 0)
                        {
                            dataAnalysisMapper.deleteForUpdate(booking.getBookingId());
                        }

                        bookingService.updateById(booking);
                    }
                    break;
                case "TRADE_CLOSED":
                    if (groupActivityMapper.selectById(outTradeNo)!=null){
                        groupActivityMapper.deleteById(outTradeNo);
                    }
                    if (booking != null && "待支付".equals(booking.getBookingStatus())) {
                        System.out.println("TRADE_CLOSED - 更新预约状态为已取消: " + outTradeNo);
                        booking.setBookingStatus("已取消");
                        bookingService.updateById(booking);
                    }
                    break;
                case "WAIT_BUYER_PAY":
                    System.out.println("订单 " + outTradeNo + " 等待买家付款");
                    break;
                default:
                    if (groupActivityMapper.selectById(outTradeNo)!=null){
                        groupActivityMapper.deleteById(outTradeNo);
                    }
                    if (booking != null && "待支付".equals(booking.getBookingStatus())) {
                        System.out.println("TRADE_CLOSED - 更新预约状态为已取消: " + outTradeNo);
                        booking.setBookingStatus("已取消");
                        bookingService.updateById(booking);
                    }
                    System.out.println("订单 " + outTradeNo + " 出现未知交易状态: " + tradeStatus);
            }
        } catch (Exception e) {
            // 记录完整的堆栈信息，便于排查问题
            System.err.println("处理支付结果消息时出现异常: ");
            e.printStackTrace();
        }
    }
}